package com.cloud.train.business.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.util.ObjectUtil;
import com.cloud.train.business.domain.DailyTrainStationExample;
import com.cloud.train.business.domain.TrainStation;
import com.cloud.train.business.domain.TrainStationExample;
import com.cloud.train.business.mapper.TrainStationMapper;
import com.cloud.train.business.req.TrainStationQueryReq;
import com.cloud.train.business.req.TrainStationSaveReq;
import com.cloud.train.business.resp.TrainStationQueryResp;
import com.cloud.train.common.exception.BusinessException;
import com.cloud.train.common.exception.BusinessExceptionEnum;
import com.cloud.train.common.resp.CommonResp;
import com.cloud.train.common.resp.PageResp;
import com.cloud.train.common.util.SnowUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class TrainStationService{
    @Resource
    private TrainStationMapper trainStationMapper;
    public void save(TrainStationSaveReq req) {
        DateTime now = DateTime.now();
        TrainStation trainStation = BeanUtil.copyProperties(req, TrainStation.class);
        if (ObjectUtil.isNull(trainStation.getId())) {

            // 保存之前，先校验唯一键是否存在
            TrainStation trainStationDB = selectByUnique(req.getTrainCode(), req.getIndex());
            if (ObjectUtil.isNotEmpty(trainStationDB)) {
                throw new BusinessException(BusinessExceptionEnum.BUSINESS_TRAIN_STATION_INDEX_UNIQUE_ERROR);
            }
            // 保存之前，先校验唯一键是否存在
            trainStationDB = selectByUnique(req.getTrainCode(),req.getIndex());
            if (ObjectUtil.isNotEmpty(trainStationDB)) {
                throw new BusinessException(BusinessExceptionEnum.BUSINESS_TRAIN_STATION_NAME_UNIQUE_ERROR);
            }

            trainStation.setId(SnowUtil.getSnowflakeNextId());
            trainStation.setCreateTime(now);
            trainStation.setUpdateTime(now);
            trainStationMapper.insert(trainStation);
        } else {
            trainStation.setUpdateTime(now);
            trainStationMapper.updateByPrimaryKey(trainStation);
        }


    }
    private TrainStation selectByUnique(String trainCode, Integer index) {
        TrainStationExample trainStationExample = new TrainStationExample();
        trainStationExample.createCriteria().andIndexEqualTo(index).andTrainCodeEqualTo(trainCode);
        List<TrainStation> trainStations = trainStationMapper.selectByExample(trainStationExample);
        if (CollUtil.isNotEmpty(trainStations)){
            return trainStations.get(0);
        }
        else {
            return null;
        }
    }

    public CommonResp<Object> delete(long id) {
        int i = trainStationMapper.deleteByPrimaryKey(id);
        CommonResp<Object> objectCommonResp = new CommonResp<>();
        if (i <= 0){
            objectCommonResp.setMessage("删除失败未知错误");
            return objectCommonResp;
        }
        else {
            return objectCommonResp;
        }
    }

    public PageResp<TrainStationQueryResp> queryList(TrainStationQueryReq req) {
        TrainStationExample trainStationExample = new TrainStationExample();
        trainStationExample.setOrderByClause("train_code asc, `index` asc");
        TrainStationExample.Criteria criteria = trainStationExample.createCriteria();
        if (ObjectUtil.isNotEmpty(req.getTrainCode())){
            criteria.andTrainCodeEqualTo(req.getTrainCode());
        }
        PageHelper.startPage(req.getPage(), req.getSize());
        List<TrainStation> trainStations = trainStationMapper.selectByExample(trainStationExample);
        PageInfo<TrainStation> trainStationQueryRespPageInfo = new PageInfo<>(trainStations);
        List<TrainStationQueryResp> trainStationQueryResps = BeanUtil.copyToList(trainStations, TrainStationQueryResp.class);
        PageResp<TrainStationQueryResp> trainStationQueryRespPageResp = new PageResp<>();
        trainStationQueryRespPageResp.setList(trainStationQueryResps);
        trainStationQueryRespPageResp.setTotal(trainStationQueryRespPageInfo.getTotal());
        return trainStationQueryRespPageResp;
    }

    /**
     * 根据火车code查询信息
     * @param trainCode
     * @return
     */
    public List<TrainStation> selectByTrainCode(String trainCode) {
        TrainStationExample trainStationExample = new TrainStationExample();
        trainStationExample.setOrderByClause("`index` asc");
        trainStationExample.createCriteria().andTrainCodeEqualTo(trainCode);
        return trainStationMapper.selectByExample(trainStationExample);
    }
}
